﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace VietPublisher.Web.Controls
{
    static public class AjaxHelperExtension
    {
        static public string ControllerName(this AjaxHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["controller"].ToString();
            //return htmlHelper.ViewContext.Controller.ValueProvider["controller"].RawValue.ToString();
        }

        static public string ActionName(this AjaxHelper htmlHelper)
        {
            return htmlHelper.ViewContext.RouteData.Values["action"].ToString();
        }

        static public MvcHtmlString Get_ActionLink(this AjaxHelper ajaxHelper, string linkText, string controller, string action)
        {   
            return AjaxExtensions.ActionLink(ajaxHelper, linkText, action, controller,
                new AjaxOptions() { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "content" });
        }

        static public MvcHtmlString Get_ActionLink(this AjaxHelper ajaxHelper, string linkText, string controller, string action, object routeValues)
        {
            return AjaxExtensions.ActionLink(ajaxHelper, linkText, action, controller, routeValues,
                new AjaxOptions() { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", UpdateTargetId = "content" });
        }

        public static string ImageActionLink(this AjaxHelper helper, string imageUrl, string altText, string actionName, object routeValues, AjaxOptions ajaxOptions)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("alt", altText);
            var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
            return link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));
        }

        public static string ButtonActionLink(this AjaxHelper helper, string textName, string actionName, object routeValues, AjaxOptions ajaxOptions)
        {
            var builder = new TagBuilder("input");
            
            builder.MergeAttribute("type", "button");
            builder.MergeAttribute("value", textName);
            var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions).ToHtmlString();
            return link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));

        }

        public static string ButtonActionLink(this AjaxHelper helper, string textName, string actionName, object routeValues, AjaxOptions ajaxOptions,object htmlAttributes)
        {
            var builder = new TagBuilder("input");

            builder.MergeAttribute("type", "button");
            builder.MergeAttribute("value", textName);
            var link = helper.ActionLink("[replaceme]", actionName, routeValues, ajaxOptions, htmlAttributes).ToHtmlString();
            
            return link.Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing));

        }

        static public string TextLinkSubmit(this AjaxHelper htmlHelper, string text, string formName,string updateId)
        {
            string click = "Sys.Mvc.AsyncForm.handleSubmit(document." + formName + ", new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, httpMethod: 'POST', updateTargetId: '" + updateId + "' });";
            return string.Format("<a onclick=\"{0}\" href=\"#\">{1}</a>",click, text);
        }

    }
}
